//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmBackwardSolver.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Experimental/Finitedifferences/FdmLinearOpComposite.h>
#include <gen/QL/Experimental/Finitedifferences/FdmStepConditionComposite.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::CFdmBackwardSolver (boost::shared_ptr<QuantLib::FdmBackwardSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBackwardSolver = NULL;
#endif
	_ppFdmBackwardSolver = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::CFdmBackwardSolver (QuantLib::FdmBackwardSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmBackwardSolver = NULL;
#endif
	_ppFdmBackwardSolver = new boost::shared_ptr<QuantLib::FdmBackwardSolver> (&childNative);
    
    _FdmBackwardSolverOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::CFdmBackwardSolver (CFdmBackwardSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBackwardSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmBackwardSolver = new boost::shared_ptr<QuantLib::FdmBackwardSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::CFdmBackwardSolver (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBackwardSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdmBackwardSolver::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::CFdmBackwardSolver (QuantLib::Handle<QuantLib::FdmBackwardSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmBackwardSolver = &childNative;
	_ppFdmBackwardSolver = &static_cast<boost::shared_ptr<QuantLib::FdmBackwardSolver>>(childNative.currentLink());
    
    _FdmBackwardSolverOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::CFdmBackwardSolver (QuantLib::Handle<QuantLib::FdmBackwardSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmBackwardSolver = &childNative;
	_ppFdmBackwardSolver = &static_cast<boost::shared_ptr<QuantLib::FdmBackwardSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::CFdmBackwardSolver (QuantLib::FdmBackwardSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBackwardSolver = NULL;
#endif
	_ppFdmBackwardSolver = new boost::shared_ptr<QuantLib::FdmBackwardSolver> (new QuantLib::FdmBackwardSolver (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::~CFdmBackwardSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmBackwardSolver != NULL)
    {
	    delete _ppFdmBackwardSolver;
        _ppFdmBackwardSolver = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::!CFdmBackwardSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmBackwardSolver != NULL)
    {
	    delete _ppFdmBackwardSolver;
    }
}
QuantLib::FdmBackwardSolver& Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::GetReference ()
{
    if (_ppFdmBackwardSolver == NULL) throw gcnew NativeNullException ();
	return **_ppFdmBackwardSolver;
}
boost::shared_ptr<QuantLib::FdmBackwardSolver>& Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::GetShared ()
{
    if (_ppFdmBackwardSolver == NULL) throw gcnew NativeNullException ();
	return *_ppFdmBackwardSolver;
}
QuantLib::FdmBackwardSolver* Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::GetPointer ()
{
    if (_ppFdmBackwardSolver == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmBackwardSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmBackwardSolver>& Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::GetHandle ()
{
	if (_phFdmBackwardSolver == NULL)
	{
		_phFdmBackwardSolver = new Handle<QuantLib::FdmBackwardSolver> (*_ppFdmBackwardSolver);
	}
	return *_phFdmBackwardSolver;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmBackwardSolver::HasNative () 
{
	return (_ppFdmBackwardSolver != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

